//
//  FinishedTaskDetailVC.m
//  rrqs
//
//  Created by mac book on 2019/5/7.
//

#import "FinishedTaskDetailVC.h"
#import "FinishOrderDetailTableViewCell.h"
#import "FinishOrderDetailInfoTableViewCell.h"
#import "FinishOrderDetailTopTableViewCell.h"
#import "FinishOrderDetailTimeTableViewCell.h"
#import "FinishOrderDetailInfoFooterView.h"

#define Cell_H  90.f/HEIGHT_6S_SCALE

@interface FinishedTaskDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView; // 主框架>

@property (nonatomic, strong) NSMutableArray *listDataArray; // 外卖数据源


@property(nonatomic,strong) UIView* earningsView;

@property (nonatomic,strong)UIButton *gukeBtn;
@property (nonatomic,strong)UIButton *shangjiaBtn;

@property(nonatomic,strong) MOrder* entity;

@end

@implementation FinishedTaskDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.title = @"订单详情";
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self creatsubViews];
    
    [self refreshDataSource];

    
}
#pragma mark =====================================================  Data source
-(void)queryData{
    AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
    NSDictionary* arg = @{
                          @"ince":@"get_order_infos",
                          @"main_id":self.main_id,
                          @"emp_id":self.Identity.userInfo.userID
                          
                          };
    [mgr POST:kHost parameters:arg progress:nil success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject) {
        [WMHelper outPutJsonString:responseObject];
        NSInteger flag = [[responseObject objectForKey:@"status"]integerValue];
        if(flag == 1){
            NSDictionary* dict = [responseObject objectForKey:@"order"];
            if(![WMHelper isNULLOrnil:dict]){
                
                self.entity = [MOrder mj_objectWithKeyValues:dict];
            }
        }else{
            [self alertHUD:[responseObject objectForKey:@"fail"]];
        }

        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
    } failure:^(NSURLSessionDataTask *_Nonnull operation, NSError * _Nonnull error) {
        [self alertHUD:@"网络异常"];
        [self.tableView.mj_header endRefreshing];
    }];
}

-(void)refreshDataSource{
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf queryData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

-(void)creatsubViews{
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [self creatFooterView];
    
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
        
    }];
    
    if (@available(iOS 11.0, *))
    {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
    }
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 33;
    
    
    [self.tableView registerClass:[FinishOrderDetailTopTableViewCell class] forCellReuseIdentifier:@"FinishOrderDetailTopTableViewCell"];
    
    [self.tableView registerClass:[FinishOrderDetailTableViewCell class] forCellReuseIdentifier:@"FinishOrderDetailTableViewCell"];
    [self.tableView registerClass:[FinishOrderDetailInfoTableViewCell class] forCellReuseIdentifier:@"FinishOrderDetailInfoTableViewCell"];
    [self.tableView registerClass:[FinishOrderDetailTimeTableViewCell class] forCellReuseIdentifier:@"FinishOrderDetailTimeTableViewCell"];

    
}


#pragma mark 提交按钮
-(UIView *)creatFooterView{
    
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    
    self.gukeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.gukeBtn addTarget:self action:@selector(gukeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.gukeBtn.layer.masksToBounds = YES;
    self.gukeBtn.layer.cornerRadius = 5;
    self.gukeBtn.titleLabel.font= kFontNameSize(16);
    [self.gukeBtn setTitle:@"联系顾客" forState:UIControlStateNormal];
    self.gukeBtn.backgroundColor=  kMyColor(248, 159, 39);
    [self.gukeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footerView addSubview:self.gukeBtn];
    
    
    
    self.shangjiaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shangjiaBtn addTarget:self action:@selector(shangjiaBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.shangjiaBtn.layer.masksToBounds = YES;
    self.shangjiaBtn.layer.cornerRadius = 5;
    self.shangjiaBtn.titleLabel.font= kFontNameSize(16);
    [self.shangjiaBtn setTitle:@"联系商家" forState:UIControlStateNormal];
    self.shangjiaBtn.backgroundColor=  kMyColor(248, 159, 39);
    [self.shangjiaBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footerView addSubview:self.shangjiaBtn];
    
    
    NSArray *masonryViewArray = @[self.gukeBtn,self.shangjiaBtn];
    
    [masonryViewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:30 leadSpacing:20 tailSpacing:20];
    
    // 设置array的垂直方向的约束
    [masonryViewArray mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(footerView).offset(10);
        make.height.mas_equalTo(40);
    }];
    
    
    
    
    
    return footerView;
}

-(void)gukeBtnAction{
    
    [self callPhone:self.entity.user_phone];
}

-(void)shangjiaBtnAction{
    [self callPhone:self.entity.main_phone];

}


#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 2){
        return self.entity.goods_info.count;
        
    }
    return 1;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        
        FinishOrderDetailTopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FinishOrderDetailTopTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.entity = self.entity;
        
        return cell;
        
    }else if (indexPath.section == 1) {
        
        FinishOrderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FinishOrderDetailTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.entity = self.entity;
        return cell;
        
    }else if (indexPath.section == 2){
        
        
        FinishOrderDetailInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FinishOrderDetailInfoTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.entity = self.entity.goods_info[indexPath.row];

        return cell;
        
    }else{
        
        FinishOrderDetailTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FinishOrderDetailTimeTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.entity = self.entity;
        
        return cell;
        
        
    }
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 2) {
        return 180;
    }else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.001;
    }else{
        return 40;
    }
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section != 0) {
        
        UIView *headerView = [[UIView alloc] init];
        headerView.backgroundColor = [UIColor whiteColor];
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [headerView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(headerView);
            make.left.equalTo(headerView).offset(15);
            make.right.equalTo(headerView).offset(-15);
            make.height.mas_equalTo(0.8);
            
        }];
        
        UILabel *title = [[UILabel alloc] init];
        [headerView addSubview:title];
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(line.mas_top);
            make.top.equalTo(headerView);
            make.left.equalTo(headerView).offset(15);
        }];
        
     
        
        
        
        if (section == 1) {
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [headerView addSubview:button];
            button.layer.masksToBounds = YES;
            button.layer.cornerRadius = 5.f;
            [button setTitle:@"配送路径" forState:UIControlStateNormal];
            [button setBackgroundColor:kMyColor(248, 159, 39)];
            button.titleLabel.font = [UIFont systemFontOfSize:12.f];
            [button addTarget:self action:@selector(connectionTouch) forControlEvents:UIControlEventTouchUpInside];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(title);
                make.right.equalTo(headerView).offset(-15);
                make.width.mas_equalTo(65);
                make.height.mas_equalTo(25);

            }];
            
            
            title.text = [NSString stringWithFormat:@"商家流水号:%@",self.entity.liushui];
            title.font = kFontNameSize(12);
            title.textColor =kMyColor(130, 130, 130);
            
        }else if (section == 2) {
            title.text = self.entity.amin_name;
            title.font = kFontNameSize(15);
            title.textColor = [UIColor darkGrayColor];
            
        }else{
            
            UILabel *content = [[UILabel alloc] init];
            content.textAlignment = NSTextAlignmentRight;
            content.textColor = [UIColor darkGrayColor];
            content.font = kFontNameSize(12);
            [headerView addSubview:content];
            [content mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(line.mas_top);
                make.top.equalTo(headerView);
                make.right.equalTo(headerView).offset(-15);
                
            }];
            
            
            title.text = @"配送耗时";
            title.font = kFontNameSize(12);
            title.textColor = [UIColor darkGrayColor];

            content.text = self.entity.yujidaoda_time;

        }
        
        return headerView;
        
        
    }
    
    return nil;
    
    
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor clearColor];
    
    if (section == 2) {
        
        FinishOrderDetailInfoFooterView *footer = [[FinishOrderDetailInfoFooterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 170)];
        footer.backgroundColor = [UIColor whiteColor];
        [footerView addSubview:footer];
        
        footer.entity = self.entity;

        

    }
    
    return footerView;
    
}

-(void) connectionTouch{
    
    [CommonUntils naviAddressWithDestination:self.entity.get_address withLatitude:self.entity.user_lat withLongitude:self.entity.user_lng withController:self];

}

/**
 *  拨打电话
 *
 *  @param phoneNumber 号码
 */
- (void)callPhone:(NSString *)phoneNumber{
    
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phoneNumber];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
    
}



#pragma mark -  property package
- (NSMutableArray *)listDataArray
{
    if (!_listDataArray) {
        _listDataArray = @[].mutableCopy;
    }
    return _listDataArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
